/*
 * 版权所有(C)，上海海鼎信息工程股份有限公司，lizhixing，所有权利保留。
 * <p>
 * 项目名：	boot-demo
 * 文件名：	UserController
 * 模块说明：
 * 修改历史：
 * 2022/1/10 - lizhixing - 创建。
 */
package com.lovely602.boot.bootdemo.controller.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lovely602.boot.bootdemo.domain.common.RestResponse;
import com.lovely602.boot.bootdemo.domain.user.User;
import com.lovely602.boot.bootdemo.service.UserService;

import io.swagger.annotations.Api;

/**
 * @author lizhixing
 */
@Api(tags = {
    "用户服务" })
@RestController
@RequestMapping(value = "ui/user/", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController {

  @Autowired
  private UserService userService;

  @PostMapping(value = "save")
  public RestResponse<String> save(@RequestBody User user) {
    User entity = userService.save(user);
    return RestResponse.success(entity.getId());
  }

  @PostMapping(value = "modify")
  public RestResponse<Void> modify(@RequestBody User user) {
    userService.modify(user);
    return RestResponse.success();
  }

  @GetMapping(value = "findById")
  public RestResponse<User> findById(@RequestParam("id") String id) {
    User entity = userService.findById(id);
    return RestResponse.success(entity);
  }

  @GetMapping(value = "findByUsername")
  public RestResponse<User> findByUsername(@RequestParam("username") String username) {
    User entity = userService.findByUsername(username);
    return RestResponse.success(entity);
  }

  @PostMapping(value = "findAll")
  public RestResponse<List<User>> findAll() {
    List<User> users = userService.findAll();
    return RestResponse.success(users);
  }

  @DeleteMapping(value = "delete")
  public RestResponse<Void> delete(@RequestParam("id") String id) {
    userService.delete(id);
    return RestResponse.success();
  }
}
